package com.java.controller;


/*
*
*
*
* */

import com.alibaba.fastjson.JSON;
import com.java.Enum.CurrentState;
import com.java.Enum.PopularityAnalysis;
import com.java.Enum.StoreBuyWay;
import com.java.Enum.StoreType;
import com.java.Service.ReceiveFileFromBefore;
import com.java.bean.TestInfoBean;
import com.java.bean.storeInfo;
import com.java.config.ApplicationConfig;
import com.java.result.Result;
import com.java.result.ResultFactory;
import com.java.util.ChineseToEnglishUtil;
import com.scala.Class.NearStoreChance;
import com.scala.Class.cal;
import com.scala.CopeWithData.CopeWithDataForNearBy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author: ZhangJin
 * @Date: 2020/9/8 8:32
 */


/*
* 1：完善数据处理日志
* 2：完善接口
*
* */

@RestController
@Slf4j
public class RedisController {

    @Autowired
    cal cal;

    @Autowired
    ApplicationConfig applicationConfig;

    @Autowired
    ReceiveFileFromBefore receiveFileFromBefore;

    @Autowired
    NearStoreChance nearStoreChance;

    public static  Long now ;

    public static String CurrentLocal;

    @RequestMapping(value = "/api/test")
    @ResponseBody
    // TODO: 2020/9/27  前端form表单 --> bean
    public Result getDataByDistinct(@ModelAttribute TestInfoBean testInfoBean){
        if(testInfoBean == null){
            return ResultFactory.buildFailResult("object is null");
        }
        testInfoBean.setStateNow(CurrentState.getState(testInfoBean.getStateNow()).getType());
        testInfoBean.setType(StoreType.getType(testInfoBean.getType()).getType());
        testInfoBean.setTitle(StoreBuyWay.getWay(testInfoBean.getTitle()).getType());
        System.out.println(testInfoBean.getLocal());
        System.out.println(testInfoBean.getDepth());
        System.out.println(testInfoBean.getWidth());
        System.out.println(testInfoBean.getTitle());
        System.out.println(testInfoBean.getType());
        CurrentLocal = testInfoBean.getLocal();
        Map<byte[], byte[]> source = cal.getSource(testInfoBean.getLocal(), "codelaterrentinfodatarentinfofor"+ChineseToEnglishUtil.getPingYin(testInfoBean.getLocal()).toLowerCase());
        if (source == null){
            return ResultFactory.buildFailResult("there is not data for this area ..");
        }
        // TODO: 2020/9/20   当前时间戳作为zset集合的名字  key score  ,传递
        Long nowTimeAsNameForZset = System.currentTimeMillis();
        now = nowTimeAsNameForZset;
        cal.calChangeStepFirst(testInfoBean,source,nowTimeAsNameForZset);
        // TODO: 2020/9/22   开启是否具有nowTimeAsNameForZset  --- com
        return ResultFactory.buildSuccessResult("success");
    }

    @RequestMapping(value = "/api/data")
    @ResponseBody
    public Result getDataByDistinct1() {
        List<TestInfoBean> ListBean = receiveFileFromBefore.AsyncMonitorResult(String.valueOf(now), CurrentLocal);
        for (TestInfoBean testInfoBean : ListBean) {
            testInfoBean.setStateNow(CurrentState.getStateByType(testInfoBean.getStateNow()).getExplain());
            testInfoBean.setType(StoreType.getTypebyType(testInfoBean.getType()).getExplain());
            testInfoBean.setTitle(StoreBuyWay.getWayByType(testInfoBean.getTitle()).getExplain());
            testInfoBean.setNumOfSeePepple(PopularityAnalysis.getExplain(testInfoBean.getNumOfSeePepple()));
        }
        return ResultFactory.buildSuccessResult(ListBean);
    }
    @RequestMapping(value = "/api/test2", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    /*json提交*/
    @CrossOrigin
    public Result getDataNearByData(@ModelAttribute TestInfoBean testInfoBean){
        if (testInfoBean == null){
            return ResultFactory.buildFailResult("object is null");
        }
        System.out.println(testInfoBean.getLocal());
        // TODO: 2020/9/22   对 Statenow  Type  Title 进行转化
        Map<byte[], byte[]> source = nearStoreChance.getSource(testInfoBean.getLocal(), "codeLaterNearByInfoDataNearByInfo"+ChineseToEnglishUtil.getPingYin(testInfoBean.getLocal()).toLowerCase());
        if (source == null){
            return ResultFactory.buildFailResult("there is not data for this area ..");
        }
        // TODO: 2020/9/24   放入redis 中
        Long nowTimeAsNameForZset = System.currentTimeMillis();
        now = nowTimeAsNameForZset;
        CurrentLocal = testInfoBean.getLocal() + "附近的商店";
        nearStoreChance.calnearByStore(testInfoBean.getLocationoflatandlong(), source,String.valueOf(nowTimeAsNameForZset));
        return ResultFactory.buildSuccessResult("success");
    }


    @RequestMapping(value = "/api/nearbydata")
    @ResponseBody
    public Result nearbydata() {
        List<storeInfo> redisDataForNearBy = receiveFileFromBefore.getRedisDataForNearBy(String.valueOf(now), CurrentLocal);
        if (redisDataForNearBy != null){
            return ResultFactory.buildSuccessResult(redisDataForNearBy);
        }
        return ResultFactory.buildFailResult("there is not data for this key");
    }

    public static void main(String[] args) {
        System.out.println(CurrentState.getState("经营中").getType());
        System.out.println(StoreType.getType("写字楼配套").getType());
        System.out.println(StoreBuyWay.getWay("转让").getType());

    }
}
